package com.fare.taluo.fetl.domain.repository.history.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.fare.taluo.fetl.domain.repository.history.ISinglePlaneInfoHistoryRepository;
import com.fare.taluo.helper.HbaseCustom;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.core.PlaneInfo;
@Repository
public class SinglePlaneInfoHistoryRepository implements
									ISinglePlaneInfoHistoryRepository {
	@Override
	public List<PlaneInfo> find(Date crawlDate, String hbaseKey,
			String fromCode, String toCode, Date flightDate) throws Exception {
		// 渠道hbasekey[5位]出发城市三字码[3位]到达城市三字码[3位]抓取时间[8位yyyyMMdd]
		// 航班时间[10位yyyyMMddHH]航班号[8位，不足则左侧补0]
		String startRowKey = String.format("%1s%2s%3s%4s%5s%6s", hbaseKey, fromCode, toCode,
				DateUtils.formatTime(crawlDate, "yyyyMMdd"),
				DateUtils.formatTime(flightDate, "yyyyMMdd"),
				"0000000000");
		String endRowKey = String.format("%1s%2s%3s%4s%5s%6s", hbaseKey, fromCode, toCode,
				DateUtils.formatTime(crawlDate, "yyyyMMdd"),
				DateUtils.formatTime(flightDate, "yyyyMMdd"),
				"ZZZZZZZZZZ");
		return HbaseCustom.findByConditionsToPlaneInfo(HbaseCustom.LP_FLIGHT_HISTORY, 
				startRowKey, endRowKey);
	}

	@Override
	public PlaneInfo find(Date crawlDate, String hbaseKey,
			String fromCode, String toCode, Date flightDate, String flightNo) throws Exception {
		// 渠道hbasekey[5位]出发城市三字码[3位]到达城市三字码[3位]抓取时间[8位yyyyMMdd]
		// 航班时间[10位yyyyMMddHH]航班号[8位，不足则左侧补0]
		String rowKey = String.format("%1s%2s%3s%4s%5s%6s", hbaseKey, fromCode, toCode,
				DateUtils.formatTime(crawlDate, "yyyyMMdd"),
				DateUtils.formatTime(flightDate, "yyyyMMddHH"),
				StringUtils.leftPad(flightNo, 8, '0'));
		return HbaseCustom.findUniqueToPlaneInfo(HbaseCustom.LP_FLIGHT_HISTORY, rowKey);
	}

}
